<template>
  <div>
    <el-form ref="myform" :model="formItem" :rules="rules" label-width="120px" size="mini">
      <el-form-item label="货品编号" prop="storckNumber">
        <el-input v-model="formItem.storckNumber" placeholder="货品编号" :maxlength="255">
          <i slot="suffix" class="el-input__icon el-icon-question" title="货品编号必须是唯一的，如果不设置货品编号，系统将按照您设置的规则自动生成货品编号" />
          <!-- </el-tooltip> -->
        </el-input>
      </el-form-item>
      <el-form-item label="备品备件分类" required prop="spareTypeCode">
        <treeselect
          v-model="formItem.spareTypeCode"
          :multiple="false"
          :options="typeOptions"
          placeholder="选择备品备件类型"
          @select="treeSelected"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="specification">
        <el-input v-model="formItem.specification" />
      </el-form-item>
      <el-form-item label="SN" required prop="sn">
        <el-input v-model="formItem.sn" placeholder="请输入SN" />
      </el-form-item>
      <el-form-item label="存放库房" required prop="storehouseId">
        <el-select v-model="formItem.storehouseId" placeholder="请选择仓库">
          <el-option v-for="(item) in storehouse" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="单价" prop="unitPrice">
        <el-input-number v-model="formItem.unitPrice" :precision="2" :step="1" :max="1000000000" />
      </el-form-item>
      <el-form-item label="货品来源">
        <el-radio-group v-model="formItem.source">
          <el-radio label="new">合同采购</el-radio>
          <el-radio label="back">退库利旧</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="关联合同">
        <el-select v-model="formItem.contractId" clearable placeholder="请选择合同">
          <el-option v-for="(item) in contracts" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {
  create,
  update,
  detail
} from '@neu/dcam-ui/api/stock'
import { list as storehouseList } from '@neu/dcam-ui/api/storehouse'
import { list as contractList } from '@neu/dcam-ui/api/contract'

import {
  tree
} from '@neu/dcam-ui/api/spareType'

// 定义一个表单原始项
const formItem = {
  /** 备件类型编码 */
  spareTypeCode: null,
  /** 备件类型名称 */
  spareTypeName: '',
  /** 规格型号 */
  specification: '',
  /** ** SN ***/
  sn: '',
  /** 存放库房id */
  storehouseId: '',
  /** 存放库房 */
  storehouseName: '',
  /** 单价 */
  unitPrice: '',
  /** 备品备件来源*/
  source: 'new',
  /** 合同ID*/
  contractId: '',
  /** 合同名称 */
  contractName: '',
  /** * 货品编号 */
  storckNumber: ''
}
export default {
  name: 'StockEditForm',
  components: {

  },
  props: {
    // 根据id可判断是新建还是更新
    id: {
      required: false,
      type: Number,
      default: -1
    }
  },
  data() {
    return {
      /** 合同信息 */
      contracts: [],
      /** 仓库 */
      storehouse: [],
      // 编辑的表单内容
      formItem: {
        ...formItem
      },
      // 表单验证规则
      rules: {
        storckNumber: [{ max: 250, message: '长度不能超过100', trigger: 'blur' }],
        storehouseId: [{ required: true, message: '选择类型仓库', trigger: 'change' }],
        spareTypeCode: [
          { required: true, message: '备品备件分类不能为空', trigger: 'change' }
        ],
        specification: [{ max: 250, message: '长度不能超过100', trigger: 'blur' }],
        sn: [{ required: true, message: 'SN不能为空', trigger: 'blur' }]
      },
      modal: true,
      /** 备件类型树 */
      typeOptions: []
    }
  },
  watch: {
    // 监控id的变化，初始化表单数据
    id: {
      handler(newVal, oldVal) {
        this.fetch()
      },
      immediate: true
    }
  },
  created() {
    contractList().then(response => {
      this.contracts = response.data
    })
    storehouseList().then(response => {
      this.storehouse = response.data
    })

    this.tree()
  },
  methods: {
    treeSelected(node) {
      this.formItem.goodsFileId = ''
      this.formItem.goodsFileName = ''
      this.formItem.specification = ''
      this.formItem.applyAssetTypeName = ''
    },
    // 类型树
    tree() {
      tree().then(response => {
        this.typeOptions = response.data
      })
    },
    // 初始化表单数据
    fetch() {
      if (this.id !== -1) {
        detail(this.id).then(response => {
          this.formItem = {
            ...response.data
          }
        })
      } else {
        this.formItem = {
          ...formItem
        }
      }
    },
    // 保存操作，返回promise，后续处理交由调用者处理
    save() {
      const updated = this.id !== -1
      return new Promise(resolve => {
        this.$refs.myform.validate((valid) => {
          if (valid) {
            if (updated) {
              resolve(update(this.id, this.formItem))
            } else {
              resolve(create(this.formItem))
            }
          } else {
            resolve(false)
          }
        })
      })
    },
    reset() {
      this.$refs.myform.resetFields()
      this.formItem = { ...formItem }
    }

  }
}
</script>
